package com.xxkj.nmip.utils;

import android.graphics.Color;
import android.util.Log;

public class ColorUtil {

    /**
     * 字符串raga(123,12,123,1) 转换成数组
     *
     * @return
     */
    public static int formtColor(String colorStr) {
        try {
            if (colorStr.indexOf("#") != -1) {
                return Color.parseColor(colorStr);
            }
            if (colorStr.indexOf("rgba") != -1) {
                String s = colorStr.split("\\(")[1].split("\\)")[0];
                String a[] = s.split(",");
                return Color.argb(getA(Float.valueOf(a[3].trim())), Integer.valueOf(a[0].trim()), Integer.valueOf(a[1].trim()), Integer.valueOf(a[2].trim()));
            }
        } catch (Exception e) {
            Log.i("ColorUtil", "=====颜色转换失败========error=");
            return 0;
        }
        return 0;
    }

    private static int getA(float a) {
        if (a >= 0.9) {
            return 255;
        } else if (a >= 0.8) {
            return 200;
        } else if (a >= 0.7) {
            return 180;
        } else if (a >= 0.6) {
            return 150;
        } else if (a >= 0.5) {
            return 120;
        } else if (a >= 0.4) {
            return 100;
        } else if (a >= 0.3) {
            return 60;
        } else if (a >= 0.2) {
            return 40;
        } else if (a >= 0.1) {
            return 20;
        } else if (a >= 0) {
            return 0;
        }
        return 255;
    }

}
